package com.xiaoma.myaudience.biz.activity;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.xiaoma.myaudience.R;
import com.xiaoma.myaudience.biz.task.DataAsyncTask;
import com.xiaoma.myaudience.biz.task.UserInfoDetailTask;
import com.xiaoma.myaudience.biz.util.Constant;
import com.xiaoma.myaudience.util.PrefHelper;
import com.xiaoma.myaudience.util.activity.BaseActivity;

public class UserInfoDetailActivity extends BaseActivity {
	private UserInfoDetailTask mUserInfoDetailTask ;
	private Context mContext;
	private ProgressDialog mUserInfoDetailDialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.personal_detail_layout);
		setNetEaseTitleBarVisibility(View.VISIBLE);
        setNetEaseTitle("个人资料");
        setNetEaseTitleGravity(Gravity.CENTER);
        setNetEaseTitleBarExtra(R.layout.video_detail_extra_layout);

        mContext = this;
        findViewById(R.id.btn_back).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				Intent intent = new Intent(UserInfoDetailActivity.this, PersonalCenterActivity.class);
				startActivity(intent);
				UserInfoDetailActivity.this.finish();
			}
		});
        
        findViewById(R.id.btn_userdetail_logout).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//PrefHelper.putBoolean(UserInfoDetailActivity.this,"islogin",false);
				Intent intent = new Intent(UserInfoDetailActivity.this,LoginActivity.class);
				startActivity(intent);
			}
		});
        //第一次加载
        mUserInfoDetailDialog = new ProgressDialog(mContext);
        mUserInfoDetailDialog.setMessage("加载中");
		mUserInfoDetailDialog.setCancelable(true);
		mUserInfoDetailDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
			@Override
			public void onCancel(DialogInterface dialog) {
				// TODO Auto-generated method stub
				cancelLoginTask();
			}
		});
        startLoginTask();
        
        
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		mUserInfoDetailDialog.setTitle("重新加载中");
		startLoginTask();
	}
	
	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		mUserInfoDetailDialog.setTitle("重新加载中");
		startLoginTask();
	}
	@Override
	public void onPreExecute(int what) {
		// TODO Auto-generated method stub
		super.onPreExecute(what);
		
		if(what==DataAsyncTask.TASK_ACTIVITY_USERINFODETAIL){
		
			if(null == mUserInfoDetailDialog){
				mUserInfoDetailDialog = new ProgressDialog(mContext);
				mUserInfoDetailDialog.setMessage("加载中");
				mUserInfoDetailDialog.setCancelable(true);
				mUserInfoDetailDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
					@Override
					public void onCancel(DialogInterface dialog) {
						// TODO Auto-generated method stub
						cancelLoginTask();
					}
				});
			}
			mUserInfoDetailDialog.show();
		}
	}

	@Override
	public void onPostExecute(int what, Object o) {
		// TODO Auto-generated method stub
		super.onPostExecute(what, o);
		if(what==DataAsyncTask.TASK_ACTIVITY_USERINFODETAIL){
			if(null != mUserInfoDetailDialog)
				mUserInfoDetailDialog.dismiss();
			
			//取出用户详细信息
						//取出用户详细信息
						Map<String, Object>  json = (Map)o;
						if(json!=null &&! (json.size()<1)){
								Boolean bx = (Boolean) json.get("islogin");
								if(bx){
									//Map<String, Object>  data = (Map<String, Object>) json.get("data");
									//Tips(json.toString());
									JSONObject userinfo = (JSONObject)json.get("userinfo");
									//发数据
									
									try {
										//是否为必填项
										boolean ba = false;
										//头像昵称
										TextView tvusername =(TextView)findViewById(R.id.tv_userdetail_username);
										String strusername =userinfo.getString("username");
										tvusername.setText(strusername);
										
										TextView tvname =(TextView)findViewById(R.id.tv_userdetail_name);
										String strname =userinfo.getString("name");
										if(strname.equals("")||strname==null){
											ba=true;
										}
										tvname.setText(strname);
										final String username = strname;
										findViewById(R.id.iv_userdetail_name).setOnClickListener(new OnClickListener() {
											
											@Override
											public void onClick(View v) {
												// TODO Auto-generated method stub
												Intent intent = new Intent(UserInfoDetailActivity.this,EditUserInfoActivity.class);
												intent.putExtra("code", "6");
												intent.putExtra("name", "姓名");
												intent.putExtra("value",username);
												startActivity(intent);
											}
										});
										TextView tvage =(TextView)findViewById(R.id.tv_userdetail_age);
										String strageyear =userinfo.getString("year");
										String stragemonth =userinfo.getString("month");
										String strageday =userinfo.getString("day");
										String strage =userinfo.getString("age");
										String strfullage = strageyear+","+stragemonth+","+strageday;
										if(strage.equals("")||strage==null){
											ba=true;
										}
										tvage.setText(strage);
										final String ageyear = strageyear;
										findViewById(R.id.iv_userdetail_age).setOnClickListener(new OnClickListener() {
											public void onClick(View v) {
												Intent intent = new Intent(UserInfoDetailActivity.this,EditUserInfoActivity.class);
												intent.putExtra("code", "8");
												intent.putExtra("name", "年龄");
												intent.putExtra("value",ageyear);
												startActivity(intent);
											}
										});
										TextView tvcarrer =(TextView)findViewById(R.id.tv_userdetail_career);
										String strcareer =userinfo.getString("zhiye");
										if(strcareer.equals("")||strcareer==null){
											ba=true;
										}
										tvcarrer.setText(strcareer);
										final String carrer =strcareer; 
										findViewById(R.id.iv_userdetail_career).setOnClickListener(new OnClickListener() {
											public void onClick(View v) {
												Intent intent = new Intent(UserInfoDetailActivity.this,EditUserInfoActivity.class);
												intent.putExtra("code", "2");
												intent.putExtra("name", "职业");
												startActivity(intent);
											}
										});
										TextView tv =(TextView)findViewById(R.id.tv_userdetail_cellphone);
										tv.setText(userinfo.getString("mobile"));
										findViewById(R.id.iv_userdetail_cellphone).setOnClickListener(new OnClickListener() {
											public void onClick(View v) {
												Intent intent = new Intent(UserInfoDetailActivity.this,EditUserInfoActivity.class);
												intent.putExtra("code", "10");
												intent.putExtra("name", "手机号码");
												startActivity(intent);
											}
										});
										
										TextView tvcompany =(TextView)findViewById(R.id.tv_userdetail_company);
										tvcompany.setText(userinfo.getString("gongsi"));
										findViewById(R.id.iv_userdetail_company).setOnClickListener(new OnClickListener() {
											@Override
											public void onClick(View v) {
												Intent intent = new Intent(UserInfoDetailActivity.this,EditUserInfoActivity.class);
												intent.putExtra("code", "4");
												intent.putExtra("name", "公司");
												startActivity(intent);
											}
										});
										tv =(TextView)findViewById(R.id.tv_userdetail_email);
										tv.setText(userinfo.getString("email"));
										findViewById(R.id.iv_userdetail_email).setOnClickListener(new OnClickListener() {
											public void onClick(View v) {
												Intent intent = new Intent(UserInfoDetailActivity.this,EditUserInfoActivity.class);
												intent.putExtra("code", "9");
												intent.putExtra("name", "邮箱");
												startActivity(intent);
											}
										});
										
										TextView tvhobby =(TextView)findViewById(R.id.tv_userdetail_hobby);
										tvhobby.setText(userinfo.getString("aihao"));
										findViewById(R.id.iv_userdetail_hobby).setOnClickListener(new OnClickListener() {
											
											@Override
											public void onClick(View v) {
												Intent intent = new Intent(UserInfoDetailActivity.this,EditUserInfoActivity.class);
												intent.putExtra("code", "3");
												intent.putExtra("name", "兴趣爱好");
												startActivity(intent);
											}
										});
										TextView tvschool =(TextView)findViewById(R.id.tv_userdetail_school);
										String strschool =userinfo.getString("xuexiao");
										if(strschool.equals("")||strschool==null){
											ba=true;
										}
										tvschool.setText(strschool);
										findViewById(R.id.iv_userdetail_school).setOnClickListener(new OnClickListener() {
											
											@Override
											public void onClick(View v) {
												// TODO Auto-generated method stub
												Intent intent = new Intent(UserInfoDetailActivity.this,EditUserInfoActivity.class);
												intent.putExtra("code", "5");
												intent.putExtra("name", "学校");
												startActivity(intent);
											}
										});
										TextView tvgender =(TextView)findViewById(R.id.tv_userinfo_gender);
										String strgender =userinfo.getString("sex");
										String gendername = strgender.equals("2")?"女":"男";
										if(strgender.equals("0")||strgender.equals("")||strgender==null){
											ba=true;
											gendername="未知";
										}
										
										tvgender.setText(gendername);
										final String gender = gendername;
										findViewById(R.id.iv_userinfo_gender).setOnClickListener(new OnClickListener() {
											
											@Override
											public void onClick(View v) {
												// TODO Auto-generated method stub
												Intent intent = new Intent(UserInfoDetailActivity.this,EditUserInfoActivity.class);
												intent.putExtra("code", "7");
												intent.putExtra("name", "性别");
												intent.putExtra("value",gender);
												startActivity(intent);
											}
										});
										
										
										//判断用户资料是否完善
										if(ba){
											Tips("请完善必填项");
											PrefHelper.putBoolean(UserInfoDetailActivity.this,"isfulluserinfo",false);
										}else{
											PrefHelper.putBoolean(UserInfoDetailActivity.this,"isfulluserinfo",true);
										}
									} catch (JSONException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}
								}

						}else{
							Tips("对不起，暂无数据");
							//Intent intent = new Intent(UserInfoDetailActivity.this, PersonalCenterActivity.class);
							//startActivity(intent);
							UserInfoDetailActivity.this.finish();
							return;
						}
						
						
					}
		
	}
	
	/**启动任务*/
	private void startLoginTask() {
		cancelLoginTask();
		mUserInfoDetailTask = new UserInfoDetailTask(mContext,
				DataAsyncTask.TASK_ACTIVITY_USERINFODETAIL, this);
		//取cookie
		Map<String,String> cookie = new HashMap<String,String>();
			cookie.put(Constant.LOGIN_COOKIE1,PrefHelper.getString(UserInfoDetailActivity.this,Constant.LOGIN_COOKIE1,""));
			cookie.put(Constant.LOGIN_COOKIE2,PrefHelper.getString(UserInfoDetailActivity.this,Constant.LOGIN_COOKIE2,""));

		mUserInfoDetailTask.execute(cookie);
	}
	
	/**关闭任务*/
	private void cancelLoginTask() {
		if (mUserInfoDetailTask != null) {
			mUserInfoDetailTask.cancel(true, true);
		}
		mUserInfoDetailTask = null;
	}
}
